
package com.example.cia.mysql.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.example.cia.mysql.IDatabaseConnection;

public class databaseConnectionImpl implements IDatabaseConnection{

	private String driverClass;
	private String url;
	private String userName;
	private String password;
	
	@Override
	public Connection getCon() throws ClassNotFoundException, SQLException {
		Class.forName(driverClass);
		Connection con =DriverManager.getConnection(url, userName, password);
		return con;
	}

	@Override
	public boolean closeCon(Connection con) {
		try{
			con.close();
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		};
		return true;
	}

	/**
	 * 初始化属性
	 * @throws IOException
	 */
	public databaseConnectionImpl() throws IOException{
		InputStream in = getClass().getClassLoader().getResourceAsStream("settings.properties");
		Properties properties = new Properties();
		properties.load(in);
		driverClass = properties.getProperty("cia.driverClass");
		url = properties.getProperty("cia.jdbcUrl");
		userName = properties.getProperty("cia.userName");
		password = properties.getProperty("cia.userPassword");
		
	}
}
